import Api from '../../constants/Api';

export default {
  namespaced: true,
  state: {
    payApplyList: [],
    payApplyCheck: [],
    payList: [],
    payCheck: [],
  },
  mutations: {
    payApplyListTo(state, data) {
      state.payApplyList = data;
    },
    payApplyCheckTo(state, data) {
      state.payApplyCheck = data;
    },
    payListTo(state, data) {
      state.payList = data;
    },
    payCheckTo(state, data) {
      state.payCheck = data;
    },
  },
  actions: {
    getPayApplyList({ commit }, { vm: that, param }) {
      that
        .$axios({
          method: 'post',
          url: Api.getPayApplyList,
          data: {
            current: that.current,
            pageSize: that.pageSize,
            approveStatus: that.approveStatus,
            contractCode: that.contractCode,
            contractName: that.contractName,
          },
        })
        .then((res) => {
          if (res.code === 20001) {
            commit(
              'payApplyListTo',
              that.current ? res.data.records : res.data
            );
            that.total = res.data.total;
            that.tableLoading = false;

            if (param) {
              if (param.infoVisible) {
                if (param.payApplyId) {
                  for (
                    let index = 0;
                    index < res.data.records.length;
                    index++
                  ) {
                    if (
                      param.payApplyId === res.data.records[index].payApplyId
                    ) {
                      that.rowData = res.data.records[index];
                    }
                  }
                } else {
                  that.rowData = res.data.records[0];
                }
                that.infoVisible = param.infoVisible;
              }
            }
          } else {
            that.tableLoading = false;
          }
        })
        .catch(() => {
          that.tableLoading = false;
        });
    },
    getPayApplyDetail({ commit }, that) {
      return that
        .$axios({
          method: 'post',
          url: Api.getPayApplyList,
          data: {
            payApplyId: that.payApplyId,
          },
        })
        .then((res) => {
          if (res.code === 20001) {
            return res.data[0];
          }
        })
        .catch(() => {});
    },
    addPayApply({ commit }, { that, param }) {
      for (const i in that.payApplyForm.acsList) {
        Reflect.deleteProperty(that.payApplyForm.acsList[i], 'uid');
        Reflect.deleteProperty(that.payApplyForm.acsList[i], 'status');
      }
      that
        .$axios({
          method: 'post',
          url: Api.addPayApply,
          data: that.payApplyForm,
        })
        .then((res) => {
          if (res.code === 20002) {
            that.handleVisible = false;
            that.$message.success(res.msg);
            that.current = 1;
            that.getData(param);
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    editPayApply({ commit }, { that, param }) {
      for (const i in that.payApplyForm.acsList) {
        Reflect.deleteProperty(that.payApplyForm.acsList[i], 'uid');
        Reflect.deleteProperty(that.payApplyForm.acsList[i], 'status');
      }
      that
        .$axios({
          method: 'post',
          url: Api.editPayApply,
          data: that.payApplyForm,
        })
        .then((res) => {
          if (res.code === 20003) {
            that.handleVisible = false;
            that.$message.success(res.msg);
            that.getData(param);
            // that.getAcsList();
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    delPayApply({ commit }, that) {
      that
        .$axios({
          method: 'delete',
          url: Api.delPayApply.replace('{payApplyId}', that.payApplyId),
        })
        .then((res) => {
          if (res.code === 20004) {
            that.delVisible = false;
            that.$message.success(res.msg);
            that.getData();
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    getChargeDown({ commit }, that) {
      return that
        .$axios({
          method: 'post',
          url: Api.getChargeDown,
          data: {
            contractId: that.contractId,
            payApplyId: that.payApplyId,
          },
        })
        .then((res) => {
          return res.data;
        })
        .catch(() => {});
    },
    submitPayApply({ commit }, that) {
      that
        .$axios({
          method: 'post',
          url: Api.submitPayApply,
          data: {
            payApplyDTO: that.payApplyForm,
            payApplyId: that.payApplyIds,
          },
        })
        .then((res) => {
          if (res.code === 20000) {
            that.handleVisible = false;
            that.$message.success(res.msg);
            that.getData();
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    getPayApplyCheck({ commit }, that) {
      that
        .$axios({
          method: 'get',
          url: Api.getPayApplyCheck,
          params: {
            payApplyId: that.payApplyId,
            current: that.subCurrent,
            pageSize: that.subSize,
          },
        })
        .then((res) => {
          if (res.code === 20001) {
            commit('payApplyCheckTo', res.data.records);
            that.subTotal = res.data.total;
          }
        })
        .catch(() => {});
    },
    payApplyRevocation({ commit }, that) {
      that
        .$axios({
          method: 'post',
          url: Api.payApplyRevocation,
          data: {
            reason: that.reason,
            targetId: that.payApplyId,
          },
        })
        .then((res) => {
          if (res.code === 20000) {
            that.backVisible = false;
            that.getData();
            that.$message.success(res.msg);
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    getPayList({ commit }, { vm: that, param }) {
      that
        .$axios({
          method: 'post',
          url: Api.getPayList,
          data: {
            current: that.current,
            pageSize: that.pageSize,
            approveStatus: that.approveStatus,
            contractCode: that.contractCode,
            contractName: that.contractName,
          },
        })
        .then((res) => {
          if (res.code === 20001) {
            commit('payListTo', that.current ? res.data.records : res.data);
            that.total = res.data.total;
            that.tableLoading = false;
            if (param) {
              if (param.infoVisible) {
                if (param.payId) {
                  for (
                    let index = 0;
                    index < res.data.records.length;
                    index++
                  ) {
                    if (
                      param.payId === res.data.records[index].payId
                    ) {
                      that.rowData = res.data.records[index];
                    }
                  }
                } else {
                  that.rowData = res.data.records[0];
                }
                that.infoVisible = param.infoVisible;
              }
            }
          } else {
            that.tableLoading = false;
          }
        })
        .catch(() => {
          that.tableLoading = false;
        });
    },
    getChargeTable({ commit }, that) {
      return that
        .$axios({
          method: 'post',
          url: Api.getChargeTable,
          data: {
            contractId: that.contractId,
            payId: that.payId,
          },
        })
        .then((res) => {
          return res.data;
        })
        .catch(() => {});
    },
    getPayApplyTable({ commit }, that) {
      return that
        .$axios({
          method: 'post',
          url: Api.getPayApplyTable,
          data: {
            contractId: that.contractId,
            payId: that.payId,
          },
        })
        .then((res) => {
          return res.data;
        })
        .catch(() => {});
    },
    addPay({ commit }, { that, param }) {
      for (const i in that.payForm.acsList) {
        Reflect.deleteProperty(that.payForm.acsList[i], 'uid');
        Reflect.deleteProperty(that.payForm.acsList[i], 'status');
      }
      if(that.payForm.sourceType ==='C'){
        that.payForm.payApplyIds = []
      }
      if(that.payForm.sourceType ==='P'){
        that.payForm.chargeAccountIds = []
      }
      that
        .$axios({
          method: 'post',
          url: Api.addPay,
          data: that.payForm,
        })
        .then((res) => {
          if (res.code === 20002) {
            that.handleVisible = false;
            that.$message.success(res.msg);
            that.current = 1;
            that.getData(param);
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    editPay({ commit }, { that, param }) {
      for (const i in that.payForm.acsList) {
        Reflect.deleteProperty(that.payForm.acsList[i], 'uid');
        Reflect.deleteProperty(that.payForm.acsList[i], 'status');
      }
      if(that.payForm.sourceType ==='C'){
        that.payForm.payApplyIds = []
      }
      if(that.payForm.sourceType ==='P'){
        that.payForm.chargeAccountIds = []
      }
      that
        .$axios({
          method: 'post',
          url: Api.editPay,
          data: that.payForm,
        })
        .then((res) => {
          if (res.code === 20003) {
            that.handleVisible = false;
            that.$message.success(res.msg);
            that.getData(param);
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    delPay({ commit }, that) {
      that
        .$axios({
          method: 'delete',
          url: Api.delPay.replace('{payId}', that.payId),
        })
        .then((res) => {
          if (res.code === 20004) {
            that.delVisible = false;
            that.$message.success(res.msg);
            that.getData();
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    paySubmitApprove({ commit }, that) {
      that
        .$axios({
          method: 'post',
          url: Api.paySubmitApprove,
          data: {
            payDTO: that.payForm,
            payId: that.payIds,
          },
        })
        .then((res) => {
          if (res.code === 20000) {
            that.handleVisible = false;
            that.$message.success(res.msg);
            that.getData();
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    payRevocation({ commit }, that) {
      that
        .$axios({
          method: 'post',
          url: Api.payRevocation,
          data: {
            reason: that.reason,
            targetId: that.payId,
          },
        })
        .then((res) => {
          if (res.code === 20000) {
            that.backVisible = false;
            that.getData();
            that.$message.success(res.msg);
          } else {
            that.$message.error(res.msg);
          }
        })
        .catch((error) => {
          that.$message.error(error.data.message);
        });
    },
    getPayCheck({ commit }, that) {
      that
        .$axios({
          method: 'get',
          url: Api.getPayCheck,
          params: {
            payId: that.payId,
            current: that.subCurrent,
            pageSize: that.subSize,
          },
        })
        .then((res) => {
          if (res.code === 20001) {
            commit('payCheckTo', res.data.records);
            that.subTotal = res.data.total;
          }
        })
        .catch(() => {});
    },
    getPayDetail({ commit }, that) {
      return that
        .$axios({
          method: 'get',
          url: Api.getPayDetail,
          params: {
            payId: that.payId,
          },
        })
        .then((res) => {
          if (res.code === 20001) {
            return res.data;
          }
        })
        .catch(() => {});
    },
  },
};
